package com.api.gbhg.lbj.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.lbj.util.Budgetsources;
import com.google.common.base.Joiner;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.api.gbhg.lbj.dao.ReimbursementDao.JinfeiQuery;
import static com.api.gbhg.lbj.tool.Tools.getuf_gnkmId;
import static com.api.gbhg.lbj.tool.Tools.getuf_gxmdygnkmId;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO TODO 报销流程会计节点金额校验(主表)
 * @date 2021/5/14 15:01
 */
@Path("/gbhg/mainCheck")
public class SubmintMainVerificationApi {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @POST
    @Path("/checkMain")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String subMainledCheck(Map<String, String> param) {
        logger.info("主表报销进入主表会计节点保存校验subMainledCheck----");
        Map<String, Object> yusuanMap=new HashMap<>();
        Budgetsources bud=new Budgetsources();
            String xzkmdm = param.get("xzkmdm");
            String nd = param.get("nd");
            String szdlhsdw = param.get("szdlhsdw");//单位
            String zcqd1 = param.get("zcqd");
            String ysly1 = param.get("ysly");
            Integer zcqd = Integer.valueOf(zcqd1);
            Integer ysly = Integer.valueOf(ysly1);
            String fzysrfl = param.get("fzysrfl");
            String ysxm = param.get("ysxm");
            String bmjjzcfl = param.get("bmjjzcfl");
            String tybxje = param.get("tybxje");
            String zcdfczbkxm = param.get("zcdfczbkxm");
            String jbbm = param.get("jbbm");
            String gnkm = getuf_gnkmId(getuf_gxmdygnkmId(xzkmdm), nd);
            bud.setZcqd(zcqd);
            bud.setYsly(ysly);
            bud.setNd(nd);
            bud.setFzysrfl(fzysrfl);
            bud.setXzkmdm(xzkmdm);
            bud.setYsxm(ysxm);
            bud.setBmjjzcfl(bmjjzcfl);
            bud.setTybxje(tybxje);
            bud.setGnkm(gnkm);
            bud.setZcdfczbkxm(zcdfczbkxm);
            bud.setDw(szdlhsdw);
            bud.setJbbm(jbbm);
            logger.info("经办部门:"+jbbm+",所属独立核实单位:"+szdlhsdw+",预算项目:"+ysxm+",功能科目:"+gnkm+",支出地方财政拨款项目"+zcdfczbkxm+",同意报销金额:" + tybxje + ",部门经济支出分类:" + bmjjzcfl + ",支出渠道:" + zcqd + ",预算来源:" + ysly + ",非中央收入分类:" + fzysrfl+",年度:"+nd);
        Map<String, Object> mapMsgly=getyusuanjinfei(bud);
        Map<String, Object> mapMsgsr=getSrjinfei(bud);
        yusuanMap.putAll(mapMsgly);
        yusuanMap.putAll(mapMsgsr);
        yusuanMap.remove("code");
        if (mapMsgly.get("code").toString().equals("200")&&mapMsgsr.get("code").toString().equals("200")){
            yusuanMap.put("code","200");
            logger.info("JSON.toJSONString(yusuanMap):"+ JSON.toJSONString(yusuanMap));
            return JSON.toJSONString(yusuanMap);
        }else {
            StringBuffer sb = new StringBuffer();
            HashMap<String,Object> TipsMap =  new HashMap<>();
            for (Map.Entry<String, Object> entry : yusuanMap.entrySet()) {
                sb.append(entry.getValue()+" |");
            }
            TipsMap.put("code","300");
            TipsMap.put("msg",sb.toString());
            logger.info("JSON.toJSONString(TipsMap):"+JSON.toJSONString(TipsMap));
            return JSON.toJSONString(TipsMap);
        }
    }
    public static Map<String, Object> getyusuanjinfei(Budgetsources bud) {
        Map<String, Object> map=new HashMap<>();
        RecordSet rs1 = JinfeiQuery(bud);
        BigDecimal tybxje = new BigDecimal(bud.getTybxje());
        Integer zcqd = bud.getZcqd();
        String channel=(zcqd==0?"中央财政收入":(zcqd==1?"非中央财政收入-财政拨款":"非中央财政收入-非财政拨款"));
        logger.info("channel---:"+channel+"rs1.getArray().size():"+rs1.getArray().size());
        if (rs1.getArray().size()!=0) {
            while (rs1.next()) {
                BigDecimal hj = new BigDecimal(rs1.getString("hj"));//合计金额
                String yslylx = rs1.getString("yslylx");
                if (tybxje.compareTo(hj) == 1) {
                    map.put("code", "300");
                    map.put("msg",channel+"-"+yslylx+"经费来源不足");//zcqd:0-中央财政收入,1-非中央财政收入-财政拨款,2-非中央财政收入-非财政拨款
                }else {
                    map.put("code", "200");
                }
            }
        }else {
            map.put("code", "300");
            map.put("msg", channel+"-"+"经费来源不存在");
        }
        return map;
    }

    public static Map<String, Object> getSrjinfei(Budgetsources bud) {
        Map<String, Object> map = new HashMap<>();
        ReimbursementDao rei = new ReimbursementDao();
        BaseBean baseBean = new BaseBean();
        int yj = Integer.parseInt(baseBean.getPropValue("Verification","yj"));//科目级
        int xj = Integer.parseInt(baseBean.getPropValue("Verification","xj"));//下级
        logger.info("科目级yj:"+yj+",下级xj:"+xj);
        String nd = bud.getNd();
        String fzysrfl = bud.getFzysrfl();
        String zcdfczbkxm = bud.getZcdfczbkxm();
        Integer zcqd = bud.getZcqd();
        String dw = bud.getDw();
        RecordSet rs = new RecordSet();
        String sql = "";
        String msg = "";
        String channel = "";
        if (zcqd == 0) {
            int sum1 = 0;
            int sum2 = 0;
            rs = ReimbursementDao.zyJinfeiQuery(bud);
            while (rs.next()) {
                String zyczbksr = rs.getString("hpssjid");//中央财政拨款收入_核批时数据id
                if (StringUtils.isNotBlank(zyczbksr)) {
                    String[] sta = zyczbksr.split("_");//当年结转核批下划线分割
                    sum1 = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                    sum2 = Integer.parseInt(sta[1]);//表id
                    if (sum1 == yj) {
                        RecordSet rs1 = rei.getzyczKm(sum2);
                        rs1.next();
                        String kmdm = rs1.getString("kmdm");
                        channel = "中央财政收入科目级-" + kmdm;
                        msg = "中央财政收入科目级-" + kmdm + "-不存在!";
                        sql = "select snjzhpsyje,hpsyje from uf_zyczbksr_km where id =" + sum2;
                        map = getCheck(bud, sql, msg, channel);
                    } else if (sum1 == xj) {
                        List<String> superiorId = rei.getSuperiorId(sum2);
                        String joinId = Joiner.on(",").join((Iterable<?>) superiorId);
                        for (int i = 0; i < joinId.split(",").length; i++) {
                            String id = joinId.split(",")[i];
                            RecordSet zyczxj = rei.getZyczxj(Integer.valueOf(id));
                            zyczxj.next();
                            String srzx = zyczxj.getString("srzx");//收入专项
                            logger.info("所有下级id" + id + ",中央财政收入专项" + srzx);
                            channel = "中央财政收入科目下级-" + srzx;
                            msg = "中央财政收入下级-" + srzx + "-不存在!";
                            sql = "select snjzhpsyje,yhpsy as hpsyje from uf_zyczbksr_xj where id =" + id;
                            map = getCheck(bud, sql, msg, channel);
                        }
                    }
                }
            }
        } else if (zcqd == 1 && fzysrfl.equals("8") || zcqd == 1 && fzysrfl.equals("5")) {
            RecordSet rs2 = rei.getDfczbkName(zcdfczbkxm);
            rs2.next();
            String xmmc = rs2.getString("xmmc");
            channel = "非中央财政收入-支出地方财政拨款项目-" + xmmc;
            msg = "非中央财政收入--支出地方财政拨款项目-" + xmmc + "-不存在!";
            sql = "select sjsrsy,snjzhpsyje from uf_fzysrxm where id=" + zcdfczbkxm + " and nf=" + nd + " and szdw=" + dw;
            map = getCheck(bud, sql, msg, channel);
        } else if (zcqd == 1) {
            RecordSet rs3 = rei.getFzyslflName(fzysrfl);
            rs3.next();
            String mc = rs3.getString("mc");
            channel = "非中央财政收入-" + mc;
            msg = "非中央财政收入-非中央收入分类-" + mc + "-不存在!";
            sql = "select snjzhpsyje,sjsrsy from uf_fzysr where szfl=" + fzysrfl + " and nf=" + nd + " and dw=" + dw;
            map = getCheck(bud, sql, msg, channel);
        }
        logger.info("收入:rs.getArray().size():" + rs.getArray().size() + ",sql---:" + sql + ",msg:" + msg + ",channel:" + channel);
        return map;
    }
    public static Map<String, Object> getCheck(Budgetsources bud,String sql,String msg,String channel){
        Map<String, Object> map=new HashMap<>();
        RecordSet rs = new RecordSet();
        Integer ysly = bud.getYsly();
        BigDecimal money = new BigDecimal(bud.getTybxje());
        Integer zcqd = bud.getZcqd();
        rs.executeQuery(sql);
        if (rs.getArray().size()!=0) {
            if (rs.next()) {
                String je="";
                String snjzhpsyje1 = rs.getString("snjzhpsyje");
                if (zcqd==0){
                    je = rs.getString("hpsyje");
                }else {
                    je = rs.getString("sjsrsy");
                }
                if (StringUtils.isBlank(je)) {
                    je = "0.00";
                }
                if (StringUtils.isBlank(snjzhpsyje1)) {
                    snjzhpsyje1 = "0.00";
                }
                BigDecimal je1 = new BigDecimal(je);//实际收入剩余金额
                BigDecimal snjzhpsyje = new BigDecimal(snjzhpsyje1);//上年结转核批剩余金额
                if (ysly == 0) {//0:当年
                    //2024年4月15日 huangwm取消实际收入判断
//                    if (money.compareTo(je1) == 1) {
//                        map.put("code", "300");
//                        map.put("msg", channel + "-" + "当年实际收入剩余金额不足");
//                    } else {
//                        map.put("code", "200");
//                    }
                    map.put("code", "200");
                } else if (ysly == 1) {//上年
                    //2024年4月15日 huangwm取消核批剩余判断
//                    if (money.compareTo(snjzhpsyje) == 1) {
//                        map.put("code", "300");
//                        map.put("msg", channel + "-" + "上年结转核批剩余金额不足");
//                    } else {
//                        map.put("code", "200");
//                    }
                    map.put("code", "200");
                }
            }
        }else {
            map.put("code", "300");
            map.put("msg", msg);
        }
        return map;
    }
}
